<template>
  <view class="point_content">
    <view class="user_title">当前积分：{{point}}</view>
    <view class="box">
      <view class="input_line">
        <!-- <view class="input_title input_title_pt">{{changeType}}数量</view> -->
        <view class="input_title input_title_pt">{{type == "1"? "增加": type == "2"? "减少":""}}数量</view>
        <view class="input_box input_box_bd">
          <AtInput
            name='name1'
            type='number'
            :border="false"
            :value="num"
            @change="changeNum"
          >
          </AtInput>
        </view>
      </view>
      <view class="input_line" v-if="open_sms_point == 1">
        <view class="input_title input_title_pt">验证码</view>
        <view class="input_box input_box_bd">
          <AtInput
            style="width: 50%;"
            type='number'
            :border="false"
            :value="verify_code"
            @change="inputVerify"
          >
          </AtInput>
          <view class="code-button" v-if="timeTrue" @tap="acquire">获取验证码</view>
          <view class="code-button" v-else>{{time}}秒后获取</view>
        </view>
      </view>
      <view class="input_line">
        <view class="input_title">备注</view>
        <view class="input_box">
          <AtTextarea
            :value="remark"
            :onChange="handleChangeRemark"
            :count="false"
          />
        </view>
      </view>
      <view class="bottom_btn" @tap="submit()">确定</view>
      <view class="back_button" @tap="back()">返回</view>
    </view>
    <AtToast :isOpened="market.isShow" :text="market.text" icon="loading"></AtToast>
  </view>
</template>

<script>
import './userPoint.scss';
import Taro from '@tarojs/taro';
import UserAPI from "../../api/user"
import Login from "../../api/login";
import { getUrlArgObject } from '../../utils/spliturl'
export default {
  props:{
    point: {
      type: String,
      default: ""
    },
    type: {
      type: String,
      default: ""
    },
    open_sms_point: {
      type: String,
      default: ""
    },
    uid: {
      type: Number,
      default: 0
    }
  },
  data(){
    return{
      timeTrue: true,   
      time: 0,
      verify_code: "",
      mobile: "",
      market: {
        isShow: false,
        text: "",
      },
      num: "",
      remark: "",
      userPoint: "",
      changeType: "",
      showPoint: false,
    }
  },
  methods: {
    changeNum(val){
      this.num = val;
    },
    handleChangeRemark(val){
      this.remark = val
    },
    // 获取用户数据
    getUserInfo() {
      // 手机号
    },
    // 发送验证码
    async send_code() {
      const res = await UserAPI.send_sms({
        app_id: Taro.getStorageSync('app_id'),
      })
      if (res.code ==  200) {
        Taro.showToast({title: res.msg, icon: "none"})
      }
    },  
    acquire() {  
      this.timeTrue = false;   
      this.time = 60;
      this.send_code(); //发送验证二维码
      var setTimeoutS = setInterval(() => {  
      this.time--;
      if (this.time <= 0) {     
        clearInterval(setTimeoutS);  
          this.timeTrue = true;      
        };
      }, 1000); 
    }, 
    async submit(){
      Taro.showToast({title: "正在提交", icon: "loading"})
      var data = {
        app_id: Taro.getStorageSync('app_id'),
        num: this.num,
        // operate_type: getUrlArgObject().type,
        operate_type: this.type,
        remark: this.remark,
        type: "2",
        // uid: getUrlArgObject().uid
        uid: this.uid
      }
      if (this.open_sms_point == 1) {
        data.verification_code = this.verify_code;
      }
      const res = await UserAPI.userAccountEdit(data);
      if(res.code == 200){
        Taro.showToast({title: "保存成功", icon: "success"})
        setTimeout(() => {
          // Taro.redirectTo({url: "menuIndex"});
          this.back(1);
        }, 1500);
      }else{
        Taro.showToast({title: res.msg, icon: "none"})
      }
    },
    inputVerify(val) {
      this.verify_code = val;
    },
    back(i){
      this.$emit('close',i)
    }
  }
}

</script>
